Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.

Add SavedModelBundle API#90

Merged
adamcrume merged 3 commits into
tensorflow:masterfrom
Enet4:savedmodelbundle
Jul 14, 2017
Merged

Add SavedModelBundle API#90
adamcrume merged 3 commits into
tensorflow:masterfrom
Enet4:savedmodelbundle

Conversation

@Enet4

@Enet4 Enet4 commented Jul 3, 2017

Copy link
Copy Markdown
Contributor

This is more of a request for feedback rather than something ready to merge now. As this concern was exposed in #68, I built an API that gathers the loaded session and meta-graph information into a bundle. When combined with the latest protobuf crate and protoc-generated sources, it is then possible to retrieve signature definitions from the meta-graph with something like this:

// load the bundle using the given properties
let mut graph = Graph::new();
let bundle = Session::from_saved_model_to_bundle(
    &SessionOptions::new(), &["train", "serve"], &mut graph, "path/to/model")?;
// read metagraph
let metagraph: MetaGraphDef = parse_from_bytes(&bundle.meta_graph_def)?;
// fetch signature definition
let signatures = metagraph.get_signature_def();

Since this crate is still independent from protobuf, this extra constructor method currently provides a vector of raw bytes.

Feedback would be certainly welcome. In particular, is the method's name ok? Should SavedModelBundle be just a tuple? Or maybe a tuple struct? This API might also depend on whether this crate will have its own protobuf serialization/deserialization code in the future. It seems that we cannot obtain certain features already available in the Python API without this.

@adamcrume

Copy link
Copy Markdown
Contributor

I originally left out proto support because TensorFlow uses proto3 and no Rust libraries at the time supported proto3. We'll have to decide at some point whether to leave serialization/deserialization to the user, or to pick from https://github.com/stepancheg/rust-protobuf or something else. If we choose a proto library, we should be able to write the API in a way that hides the choice of library from the user, but it's still a commitment. @jhseu, do you have any thoughts on TensorFlow proto concerns? I see that the Go and Java bindings expose the config proto as a raw byte array to the user.

I'd prefer any public APIs to use full-fledged structs rather than tuples or tuple structs. They have slightly more boilerplate, but they're also more extensible and explicit. Within the implementation, I think tuples and tuple structs are fine.

As for the method name, to keep more consistent with the Go API and the Java API, I think it would make sense to move it to SavedModelBundle and name it load.

@Enet4

Enet4 commented Jul 4, 2017

Copy link
Copy Markdown
Contributor Author

As for the method name, to keep more consistent with the Go API and the Java API, I think it would make sense to move it to SavedModelBundle and name it load.

That is now done in 43f6bf9.

@jhseu

jhseu commented Jul 5, 2017

Copy link
Copy Markdown

Yeah, leaving serialization/deserialization up to the user seems like the most reasonable option given the fragmented protobuf situation in Rust.

If there were a hard dependency, prost seems like the most reasonable protobuf crate right now, but it's too new to depend on.

@Enet4

Enet4 commented Jul 5, 2017

Copy link
Copy Markdown
Contributor Author

If there were a hard dependency, prost seems like the most reasonable protobuf crate right now, but it's too new to depend on.

I can't speak for prost, but rust-protobuf worked pretty well for what I'm doing. Regardless, yes, we can keep the meta-graph as a byte vector for now.

@adamcrume adamcrume left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test?

Comment thread src/session.rs Outdated
let mut status = Status::new();

let export_dir_cstr =
try!(export_dir.as_ref()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For new code, ? is preferred over try!.

Comment thread src/session.rs Outdated
pub struct SavedModelBundle {
/// The loaded session.
pub session: Session,
/// A meta graph defition as raw protocol buffer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "definition"

@Enet4

Enet4 commented Jul 9, 2017

Copy link
Copy Markdown
Contributor Author

Can you add a test?

Is it ok for me to add a "test-resources" folder with a known saved model for this?

@adamcrume

Copy link
Copy Markdown
Contributor

Sure, go ahead.

- Give explicit name to an operation in regression_savedmodel script
- Add test_resources with a saved model
- Add test for saved model bundle
- Fix typo
- Wipe try! macros
@Enet4

Enet4 commented Jul 12, 2017

Copy link
Copy Markdown
Contributor Author

The requested changes were made. 🙂

@adamcrume adamcrume left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adamcrume adamcrume merged commit 81456d3 into tensorflow:master Jul 14, 2017
@Enet4 Enet4 deleted the savedmodelbundle branch October 30, 2017 11:52
ramon-garcia pushed a commit to ramon-garcia/tensorflow-rust that referenced this pull request May 20, 2023
* tensorflow#89 Remove fold/sheet limits so a full dataset can be output, albeit still limited by DisplayMinWidth/column concatenation at header width

* Update Readme with arguments
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants